#!/bin/sh

FORM_CONFIG_STATION=/tmp/form_config/
WIFI_CONFIG_LOAD_FILE=${FORM_CONFIG_STATION}/wifi
FACTORY_CONFIG_FILE=/etc/tozed_param

FACTORY_FLAG=/etc/.flag_factory_config


if [ -f ${FACTORY_FLAG} -o ! -f /etc/config/wireless ]; then
	touch ${FACTORY_FLAG}
	mkdir -p ${FORM_CONFIG_STATION}

	#export param
	cat ${FACTORY_CONFIG_FILE} | grep TZWIFI_ > ${WIFI_CONFIG_LOAD_FILE}
	chmod 777 ${WIFI_CONFIG_LOAD_FILE}
	. ${WIFI_CONFIG_LOAD_FILE}

#######2.4g main wifi
	TZ_24G_WIFI_MAIN_DISABLE=
	TZ_24G_WIFI_MAIN_SSID=
	TZ_24G_WIFI_MAIN_PWD=

	TZ_24G_WIFI_MAIN_TX_POWER=
	TZ_24G_WIFI_MAIN_CHANNEL=
	TZ_24G_WIFI_MAIN_MAX_STATION_NUM=

	if [ "${TZWIFI_24G_MAIN_WIFI_ENABLE}" = "n" ]; then
		TZ_24G_WIFI_MAIN_DISABLE=1
	else
		TZ_24G_WIFI_MAIN_DISABLE=0
	fi

	tozed_tool -b 24g
	if [ -f /tmp/wifi_24g_ssid_pwd_file ]; then
		TZ_24G_WIFI_MAIN_SSID="`cat /tmp/wifi_24g_ssid_pwd_file | grep wifi_24g_ssid= | awk -F = '{print $2}'`"
		TZ_24G_WIFI_MAIN_PWD="`cat /tmp/wifi_24g_ssid_pwd_file | grep wifi_24g_pwd= | awk -F = '{print $2}'`"
	else
		TZ_24G_WIFI_MAIN_SSID=ZLT S21 WIFI
		TZ_24G_WIFI_MAIN_PWD=12345678
	fi

	if [ "${TZWIFI_24G_MAIN_WIFI_TX_POWER}" = "" ]; then
		TZ_24G_WIFI_MAIN_TX_POWER=100
	else
		TZ_24G_WIFI_MAIN_TX_POWER=100
	fi

	if [ "${TZWIFI_24G_MAIN_WIFI_CHANNEL}" = "" ]; then
		TZ_24G_WIFI_MAIN_CHANNEL=auto
	else
		TZ_24G_WIFI_MAIN_CHANNEL="${TZWIFI_24G_MAIN_WIFI_CHANNEL}"
	fi

	if [ "${TZWIFI_24G_MAIN_WIFI_AUTO_CHANNEL_SET}" = "y" ];then
		TZ_24G_WIFI_MAIN_CHANNEL_START=$TZWIFI_24G_MAIN_WIFI_AUTO_CHANNEL_SET_START
		TZ_24G_WIFI_MAIN_CHANNEL_END=$TZWIFI_24G_MAIN_WIFI_AUTO_CHANNEL_SET_END
	fi


	if [ "${TZWIFI_24G_MAIN_WIFI_MAX_STATION_NUM}" = "" ]; then
		TZ_24G_WIFI_MAIN_MAX_STATION_NUM=32
	else
		TZ_24G_WIFI_MAIN_MAX_STATION_NUM="${TZWIFI_24G_MAIN_WIFI_MAX_STATION_NUM}"
	fi

#######2.4g vice 1 wifi
	TZ_24G_WIFI_VICE_1_DISABLE=
	TZ_24G_WIFI_VICE_1_SSID=
	TZ_24G_WIFI_VICE_1_PWD=

	if [ "${TZWIFI_24G_VICE_1_WIFI_ENABLE}" = "y" -a "${TZ_24G_WIFI_MAIN_DISABLE}" = "0" ]; then
		TZ_24G_WIFI_VICE_1_DISABLE=0
	else
		TZ_24G_WIFI_VICE_1_DISABLE=1
	fi

	if [ "${TZWIFI_24G_VICE_1_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_24G_VICE_1_WIFI_SSID}" != "" ]; then
		TZ_24G_WIFI_VICE_1_SSID="${TZWIFI_24G_VICE_1_WIFI_SSID}"
	else
		TZ_24G_WIFI_VICE_1_SSID=${TZ_24G_WIFI_MAIN_SSID}_1
	fi

	if [ "${TZWIFI_24G_VICE_1_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_24G_VICE_1_WIFI_PWD}" != "" ]; then
		TZ_24G_WIFI_VICE_1_PWD="${TZWIFI_24G_VICE_1_WIFI_PWD}"
	else
		TZ_24G_WIFI_VICE_1_PWD=12345678
	fi

#######2.4g vice 2 wifi
	TZ_24G_WIFI_VICE_2_DISABLE=
	TZ_24G_WIFI_VICE_2_SSID=
	TZ_24G_WIFI_VICE_2_PWD=

	if [ "${TZWIFI_24G_VICE_2_WIFI_ENABLE}" = "y" -a "${TZ_24G_WIFI_MAIN_DISABLE}" = "0" ]; then
		TZ_24G_WIFI_VICE_2_DISABLE=0
	else
		TZ_24G_WIFI_VICE_2_DISABLE=1
	fi

	if [ "${TZWIFI_24G_VICE_2_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_24G_VICE_2_WIFI_SSID}" != "" ]; then
		TZ_24G_WIFI_VICE_2_SSID="${TZWIFI_24G_VICE_2_WIFI_SSID}"
	else
		TZ_24G_WIFI_VICE_2_SSID=${TZ_24G_WIFI_MAIN_SSID}_2
	fi

	if [ "${TZWIFI_24G_VICE_2_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_24G_VICE_2_WIFI_PWD}" != "" ]; then
		TZ_24G_WIFI_VICE_2_PWD="${TZWIFI_24G_VICE_2_WIFI_PWD}"
	else
		TZ_24G_WIFI_VICE_2_PWD=12345678
	fi

#######5.8g main wifi
	TZ_58G_WIFI_MAIN_DISABLE=
	TZ_58G_WIFI_MAIN_SSID=
	TZ_58G_WIFI_MAIN_PWD=

	if [ "${TZWIFI_58G_MAIN_WIFI_ENABLE}" = "n" ]; then
		TZ_58G_WIFI_MAIN_DISABLE=1
	else
		TZ_58G_WIFI_MAIN_DISABLE=0
	fi

	if [ "${TZWIFI_58G_MAIN_WIFI_SSID_PWD_LIKE_24G_SWITCH}" = "y"  ]; then
		TZ_58G_WIFI_MAIN_SSID=${TZ_24G_WIFI_MAIN_SSID}5
		TZ_58G_WIFI_MAIN_PWD=${TZ_24G_WIFI_MAIN_PWD}
	else
		tozed_tool -b 58g
		if [ -f /tmp/wifi_58g_ssid_pwd_file ]; then
			TZ_58G_WIFI_MAIN_SSID="`cat /tmp/wifi_58g_ssid_pwd_file | grep wifi_58g_ssid= | awk -F = '{print $2}'`"
			TZ_58G_WIFI_MAIN_PWD="`cat /tmp/wifi_58g_ssid_pwd_file | grep wifi_58g_pwd= | awk -F = '{print $2}'`"
		else
			TZ_58G_WIFI_MAIN_SSID=ZLT S21 WIFI 5G
			TZ_58G_WIFI_MAIN_PWD=12345678
		fi
	fi

	if [ "${TZWIFI_58G_MAIN_WIFI_MAX_STATION_NUM}" = "" ]; then
		TZ_58G_WIFI_MAIN_MAX_STATION_NUM=32
	else
		TZ_58G_WIFI_MAIN_MAX_STATION_NUM="${TZWIFI_58G_MAIN_WIFI_MAX_STATION_NUM}"
	fi

#######5.8g vice 1 wifi
	TZ_58G_WIFI_VICE_1_DISABLE=
	TZ_58G_WIFI_VICE_1_SSID=
	TZ_58G_WIFI_VICE_1_PWD=

	if [ "${TZWIFI_58G_VICE_1_WIFI_ENABLE}" = "y" -a "${TZ_58G_WIFI_MAIN_DISABLE}" = "0" ]; then
		TZ_58G_WIFI_VICE_1_DISABLE=0
	else
		TZ_58G_WIFI_VICE_1_DISABLE=1
	fi

	if [ "${TZWIFI_58G_VICE_1_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_58G_VICE_1_WIFI_SSID}" != "" ]; then
		TZ_58G_WIFI_VICE_1_SSID="${TZWIFI_58G_VICE_1_WIFI_SSID}"
	else
		TZ_58G_WIFI_VICE_1_SSID=${TZ_58G_WIFI_MAIN_SSID}_1
	fi

	if [ "${TZWIFI_58G_VICE_1_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_58G_VICE_1_WIFI_PWD}" != "" ]; then
		TZ_58G_WIFI_VICE_1_PWD="${TZWIFI_58G_VICE_1_WIFI_PWD}"
	else
		TZ_58G_WIFI_VICE_1_PWD=12345678
	fi

#######5.8g vice 2 wifi
	TZ_58G_WIFI_VICE_2_DISABLE=
	TZ_58G_WIFI_VICE_2_SSID=
	TZ_58G_WIFI_VICE_2_PWD=

	if [ "${TZWIFI_58G_VICE_2_WIFI_ENABLE}" = "y" -a "${TZ_58G_WIFI_MAIN_DISABLE}" = "0" ]; then
		TZ_58G_WIFI_VICE_2_DISABLE=0
	else
		TZ_58G_WIFI_VICE_2_DISABLE=1
	fi

	if [ "${TZWIFI_58G_VICE_2_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_58G_VICE_2_WIFI_SSID}" != "" ]; then
		TZ_58G_WIFI_VICE_2_SSID="${TZWIFI_58G_VICE_2_WIFI_SSID}"
	else
		TZ_58G_WIFI_VICE_2_SSID=${TZ_58G_WIFI_MAIN_SSID}_2
	fi

	if [ "${TZWIFI_58G_VICE_2_WIFI_STATIC_ENABLE}" = "y" -a "${TZWIFI_58G_VICE_2_WIFI_PWD}" != "" ]; then
		TZ_58G_WIFI_VICE_2_PWD="${TZWIFI_58G_VICE_2_WIFI_PWD}"
	else
		TZ_58G_WIFI_VICE_2_PWD=12345678
	fi


	cat > /etc/config/wireless <<EOF
config wifi-device  mt7603e
	option type     mt7603e
	option mode 	7
	option channel  '${TZ_24G_WIFI_MAIN_CHANNEL}'
	option txpower '${TZ_24G_WIFI_MAIN_TX_POWER}'
	option auto_channel_start '${TZ_24G_WIFI_MAIN_CHANNEL_START}'
	option auto_channel_end '${TZ_24G_WIFI_MAIN_CHANNEL_END}'
	option ht 	20
	option country CN
	option band 2.4G	
	
config wifi-iface
	option device   mt7603e
	option network	lan
	option mode     ap
	option ssid     '${TZ_24G_WIFI_MAIN_SSID}'
	option encryption psk2+ccmp
	option key      '${TZ_24G_WIFI_MAIN_PWD}'
	option maxassoc '${TZ_24G_WIFI_MAIN_MAX_STATION_NUM}'
	option disabled '${TZ_24G_WIFI_MAIN_DISABLE}'
    option wps 'pbc'

config wifi-device      mt7612e
	option type     mt7612e
	option vendor   ralink
	option band     5G
	option txpower	100
	option aregion	0
	option bw	2
	option aregion  0
	option country	CN
	option channel  auto
	option autoch   0
	option mode     15
    
config wifi-iface
	option device   mt7612e
	option ifname   rai0
	option network  lan
	option mode     ap
	option ssid     '${TZ_58G_WIFI_MAIN_SSID}'
	option encryption psk2+ccmp
	option key      '${TZ_58G_WIFI_MAIN_PWD}'
	option maxassoc '${TZ_58G_WIFI_MAIN_MAX_STATION_NUM}'
	option disabled ${TZ_58G_WIFI_MAIN_DISABLE}
    option wps 'pbc'
                           
EOF

cat > /etc/config/mutilssid <<EOF

#this four temp config must not be removed,if remove uci  will apaer id repeat bug
config temp
config temp
config temp
config temp

config wifi-iface
	option device 'mt7603e'
	option network 'lan1'
	option mode 'ap'
	option ssid '${TZ_24G_WIFI_VICE_1_SSID}'
	option encryption 'psk2+ccmp'
	option key '${TZ_24G_WIFI_VICE_1_PWD}'
	option maxassoc '20'     
	option ifname 'ra1'
	option disabled '${TZ_24G_WIFI_VICE_1_DISABLE}'

config wifi-iface
	option device 'mt7603e'
	option network 'lan2'
	option mode 'ap'
	option ssid '${TZ_24G_WIFI_VICE_2_SSID}'
	option encryption 'psk2+ccmp'
	option key '${TZ_24G_WIFI_VICE_2_PWD}'
	option maxassoc '20'
	option ifname 'ra2'
	option disabled '${TZ_24G_WIFI_VICE_2_DISABLE}'

config wifi-iface
	option device 'mt7612e'
	option network 'lan1'
	option mode 'ap'
	option ssid '${TZ_58G_WIFI_VICE_1_SSID}'
	option encryption 'psk2+ccmp'
	option key '${TZ_58G_WIFI_VICE_1_PWD}'
	option maxassoc '20'
	option ifname 'rai1'
 	option disabled '${TZ_58G_WIFI_VICE_1_DISABLE}'
                                     
config wifi-iface
	option device 'mt7612e'
	option network 'lan2'
	option mode 'ap'
	option ssid '${TZ_58G_WIFI_VICE_2_SSID}'
	option encryption 'psk2+ccmp'
	option key '${TZ_58G_WIFI_VICE_2_PWD}'
	option maxassoc '20'
	option ifname 'rai2'
	option disabled '${TZ_58G_WIFI_VICE_2_DISABLE}'
                           
EOF


fi
